package com.jy.adscm.operation.cgxsb.controller;


import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.cgxsb.bean.SporadicPurchase;
import com.jy.adscm.operation.cgxsb.service.SporadicPurchaseService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-03-12
 */
@RestController
@RequestMapping("/adscm/operation/sporadicPurchase")
public class SporadicPurchaseController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    SporadicPurchaseService sporadicPurchaseService;


    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody SporadicPurchase SporadicPurchase, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        SporadicPurchase.setCreateDate(new Date());
        SporadicPurchase.setCreator(token.getUser().getId().toString());
        SporadicPurchase.setIsReceived("-1");
        SporadicPurchase.setTaskResult("-1");
        SporadicPurchase.setTaskSelfEvaluation("-1");
        SporadicPurchase.setIsReviewTask("-1");
        SporadicPurchase.setTaskState(0);
        SporadicPurchase.setIsEveryDayTask("0");
        SporadicPurchase.setIsCreatorUpdate(1);
        boolean insert =sporadicPurchaseService.insertSporadicPurchase(SporadicPurchase);
        return new SimpleCoreResponse("200","新增成功",insert);
    }

    @RequestMapping("delete/{id}")
    public SimpleCoreResponse delete(@PathVariable Integer id, HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        boolean result = sporadicPurchaseService.deleteSporadicPurchase(id ,token);
        return new  SimpleCoreResponse("200","删除成功",result);
    }

    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody SporadicPurchase sporadicPurchase, HttpServletRequest request) throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Boolean result = sporadicPurchaseService.updateSporadicPurchase(sporadicPurchase,token);
        return  new SimpleCoreResponse("200","修改成功",result);
    }

    /**
     * 查询自己下发的零星采购任务
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("selectSporadicPurchase")
    public  SimpleCoreResponse  selectSporadicPurchase(HttpServletRequest request) throws  AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        List<SporadicPurchase> sporadicPurchases = sporadicPurchaseService.selectSporadicPurchase(token);
        return  new SimpleCoreResponse("200","查询成功",sporadicPurchases);
    }

    @RequestMapping("selectMySporadicPurchase/{taskState}")
    public  SimpleCoreResponse  selectMySporadicPurchase(@PathVariable Integer taskState , HttpServletRequest request) throws  AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        List<SporadicPurchase> sporadicPurchases = sporadicPurchaseService.selectMySporadicPurchase(taskState,token);
        return  new SimpleCoreResponse("200","查询成功",sporadicPurchases);
    }

}

